﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DMSMT;

namespace file
{
    class FileUtil
    {
        public static string SelectDir()
        {
            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    return fbd.SelectedPath;
                }
            }
            return "";
        }

        public static int FileSort(FileInfo f1, FileInfo f2)
        {
            return f2.LastWriteTime.CompareTo(f1.LastWriteTime);
        }

        public static List<FileInfo> ListFiles(string dirPath, DateTime d, string[] pattern)
        {
            DirectoryInfo dir = new DirectoryInfo(dirPath);
            List<FileInfo> list = new List<FileInfo>();
            ListFiles(list, dir, d, pattern);
            list.Sort(FileSort);
            return list;
        }

        public static string GetFileMD5(string fileName)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception e)
            {
                Log.LogError(e.Message);
                throw new Exception(fileName + "文件读取MD码失败");
            }
        }


        public static bool CopyFile(string file, string desfile)
        {
            FileStream sfs = null;
            FileStream desfs = null;
            bool success = false;
            try
            {
                sfs = new FileStream(file, FileMode.Open);
                desfs = new FileStream(file, FileMode.Create);
                byte[] data = new byte[1024];
                BufferedStream desbs = new BufferedStream(desfs);
                while (sfs.Read(data, 0, data.Length) > 0)
                {
                    desbs.Write(data, 0, data.Length);
                    desbs.Flush();
                }
                desbs.Close();
            }
            catch (Exception e)
            {
                Log.LogError(e.Message);
            }
            finally
            {
                if (sfs != null)
                {
                    sfs.Close();
                    sfs.Dispose();
                }
                if (desfs != null)
                {
                    desfs.Close();
                    desfs.Dispose();
                }
            }

            try
            {
                success = GetFileMD5(file).Equals(GetFileMD5(desfile));
            }
            catch (Exception e)
            {
                Log.LogError(e.Message);
            }

            if (success)
            {
                Log.LogText(file + "\t->\t" + desfs + "\t成功");
                return true;
            }
            else
            {
                Log.LogText(file + "\t->\t" + desfs + "\t失败");
                return false;
            }

        }

        public static void DeleteDir(string dir)
        {
            if (string.IsNullOrEmpty(dir))
            {
                MessageBox.Show("请选择目标文件目录!");
            }
            else
            {
                if (Directory.Exists(dir))
                {
                    if (MessageBox.Show("确定要清空文件目录" + dir + "?", "确认删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        try
                        {
                            DirectoryInfo d = new DirectoryInfo(dir);
                            d.Delete(true);
                            Log.LogText(dir + "目录及其子目录删除成功!");
                        }
                        catch (Exception e)
                        {
                            Log.LogError(e.Message);
                        }
                    }
                }
                else
                {
                    Log.LogText("文件目录不存在!不需要做清空操作!");
                }
            }
        }

        public static void ListFiles(List<FileInfo> list, DirectoryInfo dir, DateTime date, string[] pattern)
        {
            try
            {
                if (!dir.Exists)
                {
                    Log.LogError(dir.ToString() + "文件目录不存在!");
                    return;
                }
                foreach (string p in pattern)
                {
                    foreach (FileInfo f in dir.GetFiles(p))
                    {
                        if (f.LastWriteTime.CompareTo(date) > 0)
                            list.Add(f);
                    }
                }
                foreach (DirectoryInfo d in dir.GetDirectories())
                {
                    //if (d.LastWriteTime.CompareTo(date) > 0)
                    //{
                    //    ListFiles(list, d, date, pattern);
                    //}
                    ListFiles(list, d, date, pattern);

                }
            }
            catch (Exception e)
            {
                Log.LogError(e.Message);
            }
        }
    }
}
